<!--智慧消防V1.0-upPassword页面-->
<template>
    <div class="app-container">
        <el-row :gutter="10">
            <el-col :xs="24" :sm="18" :md="14" :lg="10" :xl="8">
                <div style="text-align: center;margin-bottom: 10px">修改密码</div>
                <el-form :model="passWordForm" ref="passWordForm" :rules="passWordFormRules" label-width="100px">
                    <el-form-item label="原密码" prop="oldPassword">
                        <el-input type="password" show-password v-model="passWordForm.oldPassword" placeholder="请输入原密码"></el-input>
                    </el-form-item>
                    <el-form-item label="新密码" prop="password">
                        <el-input type="password" show-password v-model="passWordForm.password" placeholder="请输入新密码"></el-input>
                    </el-form-item>
                    <el-form-item label="再次确认" prop="againPassWord">
                        <el-input type="password" show-password v-model="passWordForm.againPassWord" placeholder="请再次输入新密码"></el-input>
                    </el-form-item>
                </el-form>
                <div  style="text-align: center" class="dialog-footer">
                    <el-button @click="cancelUp">取 消</el-button>
                    <el-button :loading="addLoading" type="primary" @click="updatePassword">修 改</el-button>
                </div>
            </el-col>
        </el-row>
    </div>
</template>

<script>
import {updateInAdmin, updatePassword} from "@/api/enterprise";

export default {
    name: "upPassword",
    data(){
        let validatePass2 = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('请再次输入密码'))
            } else if (value !== this.passWordForm.password) {
                callback(new Error('两次输入密码不一致!'))
            } else {
                callback()
            }
        }
        return{
            passWordForm:{
                oldPassword:null,
                password:null,
                againPassWord:null
            },
            passWordFormRules:{
                oldPassword: [{ required: true, message: '原密码不能为空', trigger: 'blur' }],
                password: [{ required: true, message: '新密码不能为空', trigger: 'blur' }],
                againPassWord: [
                    { required: true, validator: validatePass2, trigger: 'blur' }
                ]
            },
            addLoading:false
        }
    },
    methods:{
        cancelUp(){
            let view = {
                fullPath: this.$route.path,
                path: this.$route.path,
                name: this.$route.name,
                meta: { ...this.$route.meta }
            }
            this.$store.dispatch('tagsView/delView', view).then(({ visitedViews }) => {
                if (view.path==this.$route.path) {
                    const latestView = visitedViews.slice(-1)[0]
                    console.log(latestView)
                    if (latestView) {
                        this.$router.push(latestView.fullPath)
                    } else {
                        // now the default is to redirect to the home page if there is no tags-view,
                        // you can adjust it according to your needs.
                        if (view.name === 'Dashboard') {
                            // to reload home page
                            this.$router.replace({ path: '/redirect' + view.fullPath })
                        } else {
                            this.$router.push('/')
                        }
                    }
                }
            })
        },
        updatePassword(){
            this.addLoading = true
            this.$refs['passWordForm'].validate(valid => {
                if (valid){
                    updatePassword(this.passWordForm).then(res=>{
                        this.addLoading = false
                        this.$notify.success({
                            title: '成功',
                            message: "更新密码成功"
                        })
                        this.logout()
                    }).catch(res=>{
                        this.addLoading = false
                        this.$notify.error({
                            title: '失败',
                            message: res.msg
                        })
                    })
                }else {
                    this.addLoading = false
                    console.log("表单验证不通过")
                }
            })
        },
        async logout() {
          await this.$store.dispatch('user/logout')
          this.$router.push(`/login?redirect=%2Fdashboard`)
        },
    }
}
</script>

<style scoped>

</style>